package com.example.yjh_login.activitys;


import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.lib_mvp.PhoneInfoUtils;
import com.example.lib_mvp.arouter.RouterUtils;
import com.example.lib_mvp.arouter.path.ArouterPath;
import com.example.lib_mvp.base.mvp.BaseMvpActivity;
import com.example.lib_mvp.httpUtil.HttpUrl;
import com.example.yjh_login.R;
import com.example.yjh_login.beans.HdLoginBean;
import com.example.yjh_login.presenterImp.LoginPresenter;
import com.tjf.lib_network.SpSaveUtils;
import com.tjf.lib_network.okhttp.ThreadManager;
import com.tjf.lib_utils.EditableTextView;
import com.tjf.lib_utils.LogsUtils;
import com.tjf.lib_utils.SpanUtils;
import com.tjf.lib_utils.TimerUtil;
import com.tjf.lib_utils.bean.BusEvent;

import java.util.TimerTask;
@Route(path = ArouterPath.PATH_LOGIN_CODE)
public class LoginCodeActivity extends BaseMvpActivity<LoginPresenter> implements View.OnClickListener {

    public View stateBar;
    public View view2;
    public ImageView loginBackImg;
    public TextView loginDltypeTv;
    public TextView phoneLoginTitle;
    public EditText loginPhoneEdt;
    public ConstraintLayout loginPhoneCly;
    public EditText loginPwdEdt;
    public TextView loginCodePhoneTv;
    public EditableTextView loginCodeEditEdt;
    public TextView loginSendCodeTv;
    public LinearLayout loginPhoneCodeLly;
    public LinearLayout linearLayout;
    public TextView loginCodeNextDlTv;
    public TextView loginPwdNextDlTv;
    int type = 1;

    String phoneNumber;
    String code;
    ;

    @Override
    protected int getLayoutId() {
        return R.layout.hd_code_login;
    }

    @Override
    protected void initView() {
        super.initView();
        initViews();
    }

    @Override
    protected void initData() {
        loginPhoneEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 1) {
                    loginCodeNextDlTv.setBackgroundResource(R.drawable.hd_shape_page_button_zise_juxing);
                } else {
                    loginCodeNextDlTv.setBackgroundResource(R.drawable.hd_shape_page_button_an_tianlan);
                }
            }
        });


    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.login_back_img) {
            Bundle bundle = new Bundle();
            bundle.putInt("authOrder", 1);
            startActivity(LoginActivity.class, bundle);
            finish();
        } else if (id == R.id.login_dltype_tv) {
            loginPhoneCly.setVisibility(View.GONE);
            loginCodeNextDlTv.setVisibility(View.GONE);
        } else if (id == R.id.login_send_code_tv) {
            if (daojishi <= 0) {
                daojishi = 60;
                getPresenter().getHdLoginSms(phoneNumber);
            }
        } else if (id == R.id.login_code_next_dl_tv) {
            if ("下一步".equals(loginCodeNextDlTv.getText())) {
                phoneNumber = loginPhoneEdt.getText().toString();
                if (TextUtils.isEmpty(phoneNumber)) {
                    showToastMsg("请输入手机号码");
                    return;
                }
                if (!PhoneInfoUtils.isMobile(phoneNumber)) {
                    showToastMsg("请输入正确的手机号码");
                    return;
                }
                phoneLoginTitle.setVisibility(View.GONE);
                loginPhoneCly.setVisibility(View.GONE);
                loginCodeNextDlTv.setVisibility(View.GONE);
                getPresenter().getHdLoginSms(phoneNumber);
                return;
            }
        } else if (id == R.id.login_pwd_next_dl_tv) {
            phoneNumber = loginPhoneEdt.getText().toString();
            if (TextUtils.isEmpty(phoneNumber)) {
                showToastMsg("请输入手机号码");
                return;
            }
            if (!PhoneInfoUtils.isMobile(phoneNumber)) {
                showToastMsg("请输入正确的手机号码");
                return;
            }

            String pwd = loginPwdEdt.getText().toString();
            if (TextUtils.isEmpty(pwd)) {
                showToastMsg("请输入密码");
                return;
            }
            getPresenter().getHdLogin(type, phoneNumber, code, pwd);
        }
    }


    LoginPresenter presenter;
    int daojishi = 60;

    @Override
    public LoginPresenter getPresenter() {
        return presenter;
    }

    @Override
    public void intPresenter() {
        presenter = new LoginPresenter();
    }


    @Override
    public void onEventBus(BusEvent event) {
        super.onEventBus(event);
        if (2 == event.getActionStatus() || 1 == event.getActionStatus()) {
            LogsUtils.i("接口异常", event.getData());
            showToast(event.getData() + "");
            if (HttpUrl.LOGIN_LOGIN.equals(event.getAction())) {
                loginCodeEditEdt.setText(null);
                checkCode();
            }
            return;
        }
        if (HttpUrl.LOGIN_SMS.equals(event.getAction())) {
            showToastMsg("验证发已发送");

            loginDltypeTv.setVisibility(View.GONE);
            loginPhoneCly.setVisibility(View.GONE);
            phoneNumber.substring(0, 3);

            SpanUtils spanUtils = new SpanUtils(this);
            SpannableStringBuilder spannableStringBuilder = spanUtils.append("+86").append("    ")
                    .append(phoneNumber.substring(0, 3)).append("   ")
                    .append(phoneNumber.substring(3, 7)).append("   ")
                    .append(phoneNumber.substring(7, 11)).create();
            loginCodePhoneTv.setText(spannableStringBuilder);

            loginPhoneCodeLly.setVisibility(View.VISIBLE);
            loginCodeEditEdt.setBoxColor(Color.parseColor("#F5F5F5"));
            Drawable drawable = getDrawable(R.drawable.cursor_color);
            loginCodeEditEdt.setTextCursorDrawable(drawable);
            loginCodeEditEdt.setOnFocusChangeListener((v, hasFocus) -> loginCodeEditEdt.setCursorVisible(hasFocus));
            checkCode();
            return;
        }
        if (HttpUrl.LOGIN_LOGIN.equals(event.getAction())) {
            LogsUtils.i("LOGIN_LOGIN=", event.getData());

            HdLoginBean hdLoginBean = (HdLoginBean) event.getData();
            SpSaveUtils.setToken(hdLoginBean.getToken() + "");
            if (hdLoginBean.getIs_activation() == 1) {
                SpSaveUtils.setActivation(hdLoginBean.getIs_activation() + "");
                RouterUtils.navigate(ArouterPath.PATH_APP_MAIN_ACTIVITY);
            } else {
                startActivity(AuthOrderActivity.class);
            }
            finish();
        }
    }

    public void checkCode() {
        TimerUtil.scheduleTask(new TimerTask() {
            @Override
            public void run() {
                ThreadManager.mMainHandler.post(() -> {
                    daojishi--;
                    if (daojishi <= 0) {
                        loginSendCodeTv.setText("重新发送");
                        loginSendCodeTv.setTextColor(getResources().getColor(R.color.hd_btn_tianlan));
                    } else {
                        loginSendCodeTv.setTextColor(getResources().getColor(R.color.gray));
                        loginSendCodeTv.setText("重新发送 " + daojishi + "s");
                    }
                    if (loginCodeEditEdt.getText().length() == 4) {
                        TimerUtil.cancelAllTasks();
                        code = loginCodeEditEdt.getText().toString();
                        getPresenter().getHdLogin(type, phoneNumber, code, "");
                    }
                });
            }
        }, 100, 1000);
    }

    private void initViews() {
        stateBar = findViewById(R.id.state_bar);
        view2 = findViewById(R.id.view2);
        loginBackImg = findViewById(R.id.login_back_img);
        loginDltypeTv = findViewById(R.id.login_dltype_tv);
        phoneLoginTitle = findViewById(R.id.phone_login_title);
        loginPhoneEdt = findViewById(R.id.login_phone_edt);
        loginPhoneCly = findViewById(R.id.login_phone_cly);
        loginPwdEdt = findViewById(R.id.login_pwd_edt);
        loginCodePhoneTv = findViewById(R.id.login_code_phone_tv);
        loginCodeEditEdt = findViewById(R.id.login_code_edit_edt);
        loginSendCodeTv = findViewById(R.id.login_send_code_tv);
        loginPhoneCodeLly = findViewById(R.id.login_phone_code_lly);
        linearLayout = findViewById(R.id.linearLayout);
        loginCodeNextDlTv = findViewById(R.id.login_code_next_dl_tv);
        loginPwdNextDlTv = findViewById(R.id.login_pwd_next_dl_tv);
        loginBackImg.setOnClickListener(this);
        loginDltypeTv.setOnClickListener(this);
        loginSendCodeTv.setOnClickListener(this);
        loginCodeNextDlTv.setOnClickListener(this);
        loginPwdNextDlTv.setOnClickListener(this);
    }
}
